import Vue from 'vue'
import VueRouter from 'vue-router'

import Home from '../views/Home.vue'
import Login from '../views/Login.vue'
import EditPost from '../views/EditPost.vue'
import PostList from '../views/PostList.vue'

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    name: 'Home',
    component: Home,
    children: [
      {
        path: 'editpost',
        component: EditPost
      },
      {
        path: 'postlist',
        component: PostList
      }
    ]
  }, {
    path: '/login',
    component: Login
  }
]

const router = new VueRouter({
  routes
})

// 路由守卫
router.beforeEach((to, from, next) => {
  // 如果是去登录页面，则不需要验证通过
  if (to.path == '/login') {
    return next()
  } else {
    if (localStorage.getItem('token')) {
      return next()
    } else {
      return router.push('/login')
    }
  }
})

export default router
